//
//  supplyLandView.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/15.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "supplyLandView.h"
#import "supplyViewButton.h"
@implementation supplyLandView
@synthesize mainScroll,mainData,isShow;
@synthesize cityStr,delegate;
-(void)dealloc{
    self.delegate = nil;
    self.mainScroll = nil;
    self.mainData = nil;
    self.cityStr = nil;
}
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.isShow = NO;
        UIScrollView *myscrollview1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 655)];
        myscrollview1.directionalLockEnabled = YES; //只能一个方向滑动
        myscrollview1.pagingEnabled = NO; //是否翻页
        myscrollview1.backgroundColor = [UIColor whiteColor];
        myscrollview1.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
        myscrollview1.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
        myscrollview1.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
        myscrollview1.delegate = self;
        self.mainScroll = myscrollview1;
        [self addSubview:self.mainScroll];
    }
    return self;
}
-(void)reloadView:(NSString *)selectCity{
    [self.mainData removeAllObjects];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"" forKey:@"id"];
    [dic setObject:@"全部" forKey:@"city"];
    [self.mainData insertObject:dic atIndex:0];
    [self.mainData addObjectsFromArray:[appStatus shareAppStatus].provinceArray];
    for (int i = 0; i <[mainData count]; i++) {
        NSDictionary *dic = [mainData objectAtIndex:i];
        NSString *nameStr = [dic objectForKey:@"city"];
        supplyViewButton *vButton = nil;
        int BtnW = (SWIDTH/3);
        int BtnH = 40;
        vButton = [self returnButtonWithFrame:CGRectMake( BtnW * (i%3) ,10+ BtnH *(i/3), BtnW, BtnH ) withTag:i+10 withTitle:nameStr];
        if ([selectCity isEqualToString:nameStr]) {
            [vButton.button setSelected:YES];
            vButton.button.titleLabel.highlightedTextColor = BLUEColor;
            vButton.button.layer.borderColor = [BLUEColor CGColor];
        }
        if ([selectCity isEqualToString:@"货源地"]) {
            if (i==0) {
                [vButton.button setSelected:YES];
                vButton.button.titleLabel.highlightedTextColor = BLUEColor;
                vButton.button.layer.borderColor = [BLUEColor CGColor];
            }   
        }
        vButton.button.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.mainScroll addSubview:vButton];
    }
    CGSize newSize = CGSizeMake(self.frame.size.width,VHEIGHT);
    [self.mainScroll setContentSize:newSize];
}
-(supplyViewButton *)returnButtonWithFrame:(CGRect)frame  withTag:(int)Tag withTitle:(NSString *)title{
    supplyViewButton *vButton = [[supplyViewButton alloc]initWithFrame:frame];
    [vButton.button setTitle:title forState:0];
    [vButton.button setTitleColor:GRAYColor forState:UIControlStateNormal];
    [vButton.button setFrame:CGRectMake((frame.size.width - 80)/2, 0, 80, 30)];
    vButton.button.titleLabel.font = [UIFont systemFontOfSize:12];
    [vButton.button setTitleColor:UIColorFromRGB(0x2578c3) forState:UIControlStateSelected];
    [vButton.button setBackgroundColor:[UIColor clearColor]];
    Radio(vButton.button, grayColor);
    [vButton.button setSelected:NO];
    vButton.button.tag = Tag;
    [vButton.button addTarget:self action:@selector(selectCity:) forControlEvents:UIControlEventTouchUpInside];
    return vButton;
}
-(void)selectCity:(UIButton *)sender{
    for (supplyLandView *vButton in [self.mainScroll subviews]) {
        for (UIButton *button in [vButton subviews]) {
            if (button.selected == YES) {
                button.selected = NO;
                sender.titleLabel.highlightedTextColor = GRAYColor;
                sender.layer.borderColor = [GRAYColor CGColor];
            }
        }
    }
    if (sender.selected == NO) {
        sender.selected = YES;
        sender.titleLabel.highlightedTextColor = BLUEColor;
        sender.layer.borderColor = [BLUEColor CGColor];
    }else{
        sender.selected = NO;
        sender.titleLabel.highlightedTextColor = GRAYColor;
        sender.layer.borderColor = [GRAYColor CGColor];
    }
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(selectSupplyLand:)]) {
        NSMutableDictionary *dic = [self.mainData objectAtIndex:sender.tag - 10];
        [self.delegate selectSupplyLand:dic];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
